﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DualTemplate.aspx.cs" Inherits="CRMWeb.Pages.Admin.DualTemplate" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Dual Template</title>
    <link rel="stylesheet" id="newtheme" type="text/css" href="/EasyUI/Themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="/Css/CrmStyle.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/EasyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript" src="/Scripts/common.js"></script>
</head>
<body>
    <div class="easyui-panel" title="Dual Template" id="pnlMain" style="width: auto; height: auto">

        <div style="float: left; width: 300px">
            <div id="pnltree" class="easyui-panel" title="Dual Category">
                <ul id="tvDualCategory" class="easyui-tree" animate="true" data-options="url:'/WebServices/DualTemplate.ashx?method=DisplayDualCategoryList&Company=' + getQuerystring('Company', ''),required:true,
                    onSelect:function(node){
                       $('#dgDualList').datagrid({
                        url: '/WebServices/DualTemplate.ashx?method=GetDualTemplateList&Company=' + getQuerystring('Company', '')+'&CategoryId='+node.id
                        });
                    },
                    onLoadSuccess:function(){
                     var rootnode = $('#tvDualCategory').tree('getRoot');
                            if (rootnode == null) {
                                $('#divDualTemplate').hide();
                                $.messager.alert('Morgan CRM', 'No Dual Template available ' + usrCompany, 'info');
                            }
                            else {
                                $('#divDualTemplate').show();
                                $('#tvDualCategory').tree('select', rootnode.target);
                            }
                            $('#dgDualList').datagrid('resize', {
                            width: window.innerWidth - 310,
                            height: window.innerHeight - 30
                            }); 
                    }">
                </ul>
            </div>
        </div>
        <div id="divDualTemplate" style="width: 450px">
            <table id="dgDualList" style="width: auto; height: 640px; overflow-y: auto;"
                toolbar="#toolbarList" pagination="true" data-options="pageSize:20"
                rownumbers="true" fitcolumns="true" singleselect="true" autorowheight="true">
                <thead>
                    <tr>
                        <th field="DualTemplate" align="center">TemplateName</th>
                        <th field="DualCompany" align="center">Company</th>
                        <th field="DualSubject" width="400px" align="center">Subject</th>
                    </tr>
                </thead>
            </table>
            <div id="toolbarList">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newDualTemplate()">New</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editDualTemplate()">Edit</a>
                <a id="lnkDelete" href="javascript:void(0)" class="easyui-linkbutton" onclick="openDelDualPopup()" plain="true" iconcls="icon-remove" title="Delete Notice">Delete</a>
            </div>

        </div>
    </div>
    <div id="dlgDualTemp" class="easyui-dialog" style="width: 600px; height: 650px; padding: -3px"
        closed="true" buttons="#dlg-buttons">

        <input id="hdnDualTemplateID" type="hidden" />
        <input id="hdnEmailSub" type="hidden" />
        <input id="hdnSMSCategoryID" type="hidden" />
        <input id="hdnEmailCategoryID" type="hidden" />
        <form id="fmDItem" method="post" novalidate>

            <div class="fitem" style="margin-top: 9px;">
                <label style="width: 100px; margin-left: 10px; text-align: left">Dual Template:</label>
                <span id="spncmbDualTemplate">
                    <input class="easyui-combotree" value="" id="cmbDualTemplate" style="width: 450px;"
                        data-options="url:'/WebServices/DualTemplate.ashx?method=DisplayDualTemplateNode&Company=' + getQuerystring('Company', ''),id: 'DualTemplateID',
		text: 'DualTemplate',required:true, onSelect:selectDualTemplate " />
                </span>
                <span id="spntxtDualTemplate">
                    <input id="txtNewDualTemplate" class="easyui-validatebox" required="true" />
                </span>
            </div>


            <div class="fitem">
                <label style="width: 100px; margin-left: 10px; text-align: left">E-Mail Template:</label>
                <span id="spncmbEmailTemplate">

                    <input class="easyui-combotree" value="" id="cmbEmailTemplate" style="width: 450px;"
                        data-options="url:'/WebServices/EmailTemplate.ashx?method=DisplayTemplateNode&Company=' + getQuerystring('Company', ''),id: 'CategoryId',
		text: 'EmailTemplate',required:true, onSelect:selectEmailTemplate " />


                </span>
            </div>


            <div class="fitem">
                <label style="width: 100px; margin-left: 10px; text-align: left">SMS Template:</label>
                <span id="spncmbSmsTemplate">

                    <input class="easyui-combotree" value="" id="cmbSmsTemplate" style="width: 300px;"
                        data-options="url:'/WebServices/SMSTemplate.ashx?method=DisplayTemplateNode&Company=' + getQuerystring('Company', ''),id: 'CategoryId',
		text: 'SMSTemplate',required:true, onSelect:selectSMSTemplate " />


                </span>
            </div>


            <div class="fitem">
                <span id="spnCategory" style="display: none">
                    <label style="width: 100px; margin-left: 10px; text-align: left">Category:</label>
                    <select class="easyui-combotree" id="cmbCategory" style="width: 450px;"
                        data-options="url:'/WebServices/DualTemplate.ashx?method=DisplayDualCategoryList&Company=' + getQuerystring('Company', ''),required:true">
                    </select>
                </span>
            </div>


            <div class="fitem">
                <label style="width: 100px; margin-left: 10px; text-align: left">Company:</label>
                <input class="easyui-combobox" id="cmbDualRole" style="width: 450px;"
                    data-options="panelWidth:450, 
                     multiple:true,
                             editable:false,
                        valueField: 'MemberGroupName',  
                        textField: 'MemberGroupName',
                        url: '/WebServices/Notice.ashx?method=BindAllRole',
                    selected:true" />
            </div>


            <div class="fitem">
                <label style="width: 100px; margin-left: 10px; text-align: left">Subject:</label>
                <input id="txtDualTempSub" name="txt_sub" type="text" style="width: 300px;" class="easyui-validatebox" required="true" />
            </div>


            <div class="fitem">
                <label style="width: 150px; margin-left: 10px">E-Mail Template Format:</label>
            </div>
            <div class="fitem" style="padding: 10px;">

                <%--<div data-options="region:'center'" style="padding: 10px;height:100%"  class="emailbox">--%>
                <textarea id="txtEmailFormat" name="MailDescription" class="easyui-validatebox" style="width: 100%; height: 410px"></textarea>
            </div>
            <div class="fitem">
                <label style="width: 150px; margin-left: 10px">SMS Template Format:</label>
            </div>
            <div data-options="region:'center'" style="padding: 10px; height: 11%" class="smsbox">
                <textarea id="txtSmsFormat" name="SMSDescription" class="easyui-validatebox" style="width: 100%; height: 85%"></textarea>
            </div>
            <input type="hidden" id="hdnDualRole" runat="server" />

        </form>
    </div>


    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgDualTemp').dialog('close')" style="float: left">Cancel</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="SaveDualTemplate()">Save</a>
    </div>
    <div id="dlgDelDualList" class="easyui-dialog" title="Confirm" style="width: 300px; height: 160px" closed="true" resizable="false" modal="true">
        <h4 style="text-align: center">Are you sure want to delete this template?</h4>
        <div class="fitem" style="text-align: center; padding: 10px; margin-top: 10px">
            <a id="lnkDelDualList" href="javascript:void(0)" class="easyui-linkbutton" onclick="deleteDualList()" disabled="true">OK</a>
            <a id="lnkCancleDualList" href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#dlgDelDualList').dialog('close')" disabled="true">Cancel</a>
            <span id="spnanimate">
                <img id="deleteProgress" style="margin-left: 20px" src="../../Media/loading.gif" />
                <label id="lbltimeOutn" style="margin-left: 20px">5</label>
            </span>
        </div>
    </div>
</body>
<script type="text/javascript">


    function getQuerystring(key, defaultValue) {
        if (defaultValue == null) defaultValue = "";
        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if (qs == null)
            return defaultValue;
        else {
            return qs[1];
        }
    }

    window.onresize = function () {
        $('#pnlMain').panel('resize', {
            width: window.innerWidth - 2,
            height: window.innerHeight - 2
        });
        $('#pnltree').panel('resize', {
            height: window.innerHeight - 35
        });
        $('#dgDualList').datagrid('resize', {
            width: window.innerWidth - 315,
            height: window.innerHeight - 35
        });
    }


    $('#dlgDelDualList').dialog({
        onOpen: function () {
            startCounter(5);
        },
        onClose: function () {
            $('#lnkDelDualList').linkbutton('disable');
            $('#lnkCancleDualList').linkbutton('disable');
            $('#spnanimate').show();
            $('#lbltimeOutn').html('');
        }
    });


    function startCounter(limit) {
        setTimeout(function () {
            if (limit == 0) {
                $('#lnkDelDualList').linkbutton('enable');
                $('#lnkCancleDualList').linkbutton('enable');
                $('#spnanimate').hide();
            }
            else {
                $('#lbltimeOutn').html(limit);
                startCounter(limit - 1);
            }
        }, 1000);
    }


    $(document).ready(function () {
        //Change Theme:
        $('#newtheme').attr('href', '/EasyUI/Themes/' + getQuerystring('Theme', '') + '/easyui.css');
        $('#dgDualList').datagrid({
            onDblClickRow: function (index, rowdata) {
                editDualTemplate();
            }
        });
        $('#cmbDualTemplate').combotree({
            onClick: function (node) {
                if (!$('#cmbDualTemplate').tree('isLeaf', node.target)) {
                    $('#cmbDualTemplate').combotree('setValues', '');
                    var t = $('#cmbDualTemplate').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#cmbDualTemplate').combotree('panel');
                    p.panel('open');
                }
            }
        });
        $('#cmbEmailTemplate').combotree({
            onClick: function (node) {
                if (!$('#cmbEmailTemplate').tree('isLeaf', node.target)) {
                    $('#cmbEmailTemplate').combotree('setValues', '');
                    var t = $('#cmbEmailTemplate').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#cmbEmailTemplate').combotree('panel');
                    p.panel('open');
                }
            }
        });
        $('#cmbSmsTemplate').combotree({
            onClick: function (node) {
                if (!$('#cmbSmsTemplate').tree('isLeaf', node.target)) {
                    $('#cmbSmsTemplate').combotree('setValues', '');
                    var t = $('#cmbSmsTemplate').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#cmbSmsTemplate').combotree('panel');
                    p.panel('open');
                }
            }
        });
        $('#cmbCategory').combotree({
            onClick: function (node) {

                if (!$('#cmbCategory').tree('isLeaf', node.target)) {
                    $('#cmbCategory').combotree('setValues', '');
                    var t = $('#cmbCategory').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#cmbCategory').combotree('panel');
                    p.panel('open');
                }
            }
        });


        $('#pnltree').panel('resize', {
            height: window.innerHeight - 30
        });
        $('#spntxtDualTemplate').hide();
        $('#txtNewDualTemplate').val('');
        $('#pnlDualTemplate').panel('resize', {
            width: window.innerWidth,
            height: window.innerHeight
        });
        $('#cmbEmailTemplate').combobox('textbox').bind('click', function () {
            $('#cmbEmailTemplate').combobox('showPanel');
        });

        $('#cmbDualRole').combobox('textbox').bind('click', function () {
            $('#cmbDualRole').combobox('showPanel');
        });

        $('#cmbSmsTemplate').combobox('textbox').bind('click', function () {
            $('#cmbSmsTemplate').combobox('showPanel');
        });
        $('#cmbDualTemplate').combobox('textbox').bind('click', function () {
            $('#cmbDualTemplate').combobox('showPanel');
        });
        $('#cmbCategory').combobox('textbox').bind('click', function () {
            $('#cmbCategory').combobox('showPanel');
        });

        $('#lnkdeleteDualTemplate').hide();
        $('#lnkcancelDualTemplate').hide();
    });



    function selectEmailTemplate(node) {
        var currentTemplateId = node.id;

        if (currentTemplateId == -1) {
            $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
            $('#cmbEmailTemplate').combotree('setValue', '');
        }
        else {

            $.ajax({
                url: "/WebServices/EmailTemplate.ashx?method=GetEmailTemplateContentById",
                contentType: "application/json; charset=utf-8",
                data: { EmailTemplateId: currentTemplateId },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {

                        tinymce.get('txtEmailFormat').setContent(decodeURIComponent(result.TemplateFormat));
                        $('#hdnEmailSub').val(result.emailsubject);
                        $('#hdnEmailCategoryID').val(result.CategoryId);


                    });
                }
            });
        }
    }

    function selectSMSTemplate(node) {

        var currentTemplateId = node.id;

        if (currentTemplateId == -1) {
            $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
            $('#cmbSmsTemplate').combotree('setValue', '');
        }
        else {
            $.ajax({
                url: "/WebServices/SMSTemplate.ashx?method=GetSMSTemplateContentById",
                contentType: "application/json; charset=utf-8",
                data: { smsTemplateId: currentTemplateId },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {
                        $('#txtSmsFormat').val(result.TemplateFormat);
                        $('#hdnSMSCategoryID').val(result.CategoryId);

                    });
                }
            });
        }
    }

    function selectDualTemplate(node) {

        var dualTemplateId = node.id;
        if (dualTemplateId == -1) {
            $('#cmbDualTemplate').combotree('setValue', '');
            $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
        }
        else {
            $('#lnkdeleteDualTemplate').show();
            $.ajax({
                url: "/WebServices/DualTemplate.ashx?method=GetDualTemplateContentById",
                contentType: "application/json; charset=utf-8",
                data: { DualTemplateId: dualTemplateId },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {

                        $('#cmbCategory').combotree('setValue', result.CategoryId);
                        $('#hdnDualTemplateID').val(dualTemplateId);
                        $('#cmbEmailTemplate').combotree('setValue', result.EmailTemplateID);
                        $('#cmbSmsTemplate').combotree('setValue', result.SmsTemplateID);
                        $('#txtDualTempSub').val(result.DualSubject);
                        var defDualRole = result.DualCompany;
                        if (defDualRole != null) {
                            var setDualRole = defDualRole.split(',');

                            $('#cmbDualRole').combobox('setValues', setDualRole);
                        }
                        else {
                            $('#cmbDualRole').combobox('setValues', '');
                        }
                    });
                }
            });
        }
    }

    function newDualTemplate() {
        $('#dlgDualTemp').dialog('open');

        $('#dlgDualTemp').panel('setTitle', 'Create New Dual Template');
        $('#cmbDualRole').combobox({
            onLoadSuccess: function (data) {
                $(this).combobox('select', data[0]['MemberGroupName']);
            },
        });
        $('#spncmbDualTemplate').hide();
        $('#spntxtDualTemplate').show();
        $('#cmbCategory').combotree('reload');
        $('#spnCategory').show();
        $('#txtNewDualTemplate').show();
        $('#lnknewDualTemplate').hide();
        $('#lnkdeleteDualTemplate').hide();
        $('#lnkcancelDualTemplate').show();
        resetForm();
    }

    function editDualTemplate() {


        var row = $('#dgDualList').datagrid('getSelected');
        if (row) {
            $('#dlgDualTemp').dialog('open');
            $('#lnknewDualTemplate').show();
            $('#txtNewDualTemplate').hide();
            $('#spncmbDualTemplate').show();
            $('#dlgDualTemp').panel('setTitle', 'Edit Dual Template');
            var dualTemplateId = row.DualTemplateID;

            if (dualTemplateId == -1) {
                $('#cmbDualTemplate').combotree('setValue', '');
                $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
            }
            else {
                $.ajax({
                    url: "/WebServices/DualTemplate.ashx?method=GetDualTemplateContentById",
                    contentType: "application/json; charset=utf-8",
                    data: { DualTemplateId: dualTemplateId },
                    success: function (data) {
                        $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {

                            tinymce.get('txtEmailFormat').setContent(decodeURIComponent(result.Emailformat));
                            $('#txtSmsFormat').val(result.Smsformat);
                            $('#cmbCategory').combotree('setValue', result.CategoryId);
                            $('#cmbDualTemplate').combotree('setValue', row.DualTemplateID);
                            $('#hdnDualTemplateID').val(dualTemplateId);
                            $('#cmbEmailTemplate').combotree('setValue', result.EmailTemplateID);
                            $('#cmbSmsTemplate').combotree('setValue', result.SmsTemplateID);
                            $('#txtDualTempSub').val(result.DualSubject);
                            var defDualRole = result.DualCompany;
                            if (defDualRole != null) {
                                var setDualRole = defDualRole.split(',');

                                $('#cmbDualRole').combobox('setValues', setDualRole);
                            }
                            else {
                                $('#cmbDualRole').combobox('setValues', '');
                            }
                        });
                    }
                });
            }
        }
    }


    function resetForm() {
        $('#cmbDualTemplate').combotree('reload');
        $('#cmbEmailTemplate').combotree('reload');
        $('#cmbSmsTemplate').combotree('reload');
        $('#cmbDualTemplate').combotree('setValue', '');
        $('#cmbEmailTemplate').combotree('setValue', '');
        $('#cmbSmsTemplate').combotree('setValue', '');
        $('#cmbDualRole').combobox('setValues', '');
        $('#txtNewDualTemplate').val('');
        $('#txtDualTempSub').val('');
        tinymce.get('txtEmailFormat').setContent('');
        $('#txtSmsFormat').val('');
        $('#hdnDualTemplateID').val(0);
    }
    function cancelDualTemplate() {
        $('#dlgDualTemp').panel('setTitle', ' Dual Template');
        $('#dgDualList').datagrid('reload');
        $('#dlgDualTemp').dialog('close');
        $('#spncmbDualTemplate').show();
        $('#spntxtDualTemplate').hide();
        $('#spnCategory').hide();
        $('#lnknewDualTemplate').show();
        $('#lnkcancelDualTemplate').hide();
        $('#lnkdeleteDualTemplate').hide();
        resetForm();
    }
    tinyMCE.init({
        mode: "exact",
        elements: "txtEmailFormat",
        theme: "advanced",
        plugins: "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,help,code,|,forecolor,backcolor",
        setup: function (ed) {
            ed.onInit.add(function (ed) {
                ed.getDoc().body.style.fontSize = '13pt';
            });
        }
    });


    //Validate required fields
    function IsValidate() {
        var flag = true;
        if ($('#txtNewDualTemplate').is(':visible') == true && $('#txtNewDualTemplate').val() == '') {
            $.messager.alert('Morgan CRM', 'Template name is required.', 'warning');
            return flag = false;
        }

        var tremail = $('#cmbEmailTemplate').combotree('tree');
        var emailtrNode = tremail.tree('getSelected');

        if (emailtrNode == null) {
            $.messager.alert('Morgan CRM', 'Please select email template... !', 'warning');
            return flag = false;
        }

        var trsms = $('#cmbSmsTemplate').combotree('tree');
        var smstrNode = trsms.tree('getSelected');
        if (smstrNode == null) {
            $.messager.alert('Morgan CRM', 'Please select sms template... !', 'warning');
            return flag = false;
        }

        var trCat = $('#cmbCategory').combotree('tree');
        var CategorynodeTr = trCat.tree('getSelected');

        if (CategorynodeTr == null) {
            $.messager.alert('Morgan CRM', 'Please select category... !', 'warning');
            return flag = false;
        }
        if ($('#txtDualTempSub').val() == '') {
            $.messager.alert('Morgan CRM', 'Subject is required.', 'warning');
            return flag = false;
        }
        return flag;
    }


    function SaveDualTemplate() {
        if (IsValidate()) {
            var SmsTemplateFormat = $('#txtSmsFormat').val();
            var EmailTemplateFormat = encodeURIComponent(tinyMCE.get('txtEmailFormat').getContent());
            var DualCompany = $('#cmbDualRole').combobox('getText');
            var TemplateId = $('#hdnDualTemplateID').val();
            var TemplateName = '';
            if (TemplateId != 0)
                TemplateName = $('#cmbDualTemplate').combobox('getText');
            else
                TemplateName = $('#txtNewDualTemplate').val();


            var temail = $('#cmbEmailTemplate').combotree('tree');
            var emailNode = temail.tree('getSelected');

            var tsms = $('#cmbSmsTemplate').combotree('tree');
            var smsNode = tsms.tree('getSelected');

            var tr = $('#cmbCategory').combotree('tree');
            var Categorynode = tr.tree('getSelected');


            $.ajax({
                url: '/WebServices/DualTemplate.ashx?method=saveDualTemplate',
                data: { DualTemplateID: TemplateId, DualSubject: $('#txtDualTempSub').val(), DualTemplate: TemplateName, SmsTemplateID: smsNode.id, EmailTemplateID: emailNode.id, CategoryID: Categorynode.id, DualCompany: DualCompany },
                success: function (data) {
                    if (data == 0) {
                        $.messager.alert('Morgan CRM', 'Dual template already exists.', 'info');
                    }
                    else {
                        //Start
                        if (TemplateId != 0) {

                            if (smsNode.id != '') {
                                if (SmsTemplateFormat != '') {
                                    $.ajax({
                                        url: '/WebServices/SMSTemplate.ashx',
                                        type: 'POST',
                                        data: {
                                            method: 'UpdateSMSTemplateDual',
                                            args: { SMSTemplateID: smsNode.id, TemplateFormat: SmsTemplateFormat, CategoryID: $('#hdnSMSCategoryID').val() }
                                        }
                                    });
                                }
                            }

                            if (emailNode.id != '') {
                                if (EmailTemplateFormat != '') {
                                    $.ajax({
                                        url: '/WebServices/EmailTemplate.ashx',
                                        type: 'POST',
                                        data: {
                                            method: 'UpdateEmailTemplateDual',
                                            args: { EmailId: emailNode.id, TemplateFormat: EmailTemplateFormat, CategoryID: $('#hdnEmailCategoryID').val() }
                                        }
                                    });
                                }
                            }

                            $.messager.alert('Morgan CRM', 'Dual template updated successfully.');
                            cancelDualTemplate();
                        }
                        else {

                            if (smsNode.id != '') {
                                if (SmsTemplateFormat != '') {
                                    $.ajax({
                                        url: '/WebServices/SMSTemplate.ashx',
                                        type: 'POST',
                                        data: {
                                            method: 'UpdateSMSTemplateDual',
                                            args: { SMSTemplateID: smsNode.id, TemplateFormat: SmsTemplateFormat, CategoryID: $('#hdnSMSCategoryID').val() }
                                        }
                                    });
                                }
                            }

                            if (emailNode.id != '') {
                                if (EmailTemplateFormat != '') {
                                    $.ajax({
                                        url: '/WebServices/EmailTemplate.ashx',
                                        type: 'POST',
                                        data: {
                                            method: 'UpdateEmailTemplateDual',
                                            args: { EmailId: emailNode.id, TemplateFormat: EmailTemplateFormat, CategoryID: $('#hdnEmailCategoryID').val() }
                                        }
                                    });
                                }
                            }

                            $.messager.alert('Morgan CRM', 'New dual template created.');
                            cancelDualTemplate();
                        }

                        //End
                    }
                }
            });
        }

    }
    function deleteDualTemplate() {
        var TemplateId = $('#hdnDualTemplateID').val();
        $.ajax({
            url: '/WebServices/DualTemplate.ashx?method=deleteDualTemplate',
            data: { DualTemplateID: TemplateId },
            success: function (data) {
                $.messager.alert('Morgan CRM', 'Dual template deleted.');
                cancelDualTemplate();
            }
        });
    }


    //Load all list items:


    function openDelDualPopup() {
        var row = $('#dgDualList').datagrid('getSelected');
        if (row) {
            $('#dlgDelDualList').dialog('open');

        }
    }


    //delete list data:
    function deleteDualList() {
        var row = $('#dgDualList').datagrid('getSelected');
        if (row) {
            $.ajax({
                url: '/WebServices/DualTemplate.ashx',
                type: 'POST',
                data: {
                    method: 'DeleteDualListData',
                    args: { DualTemplateID: row.DualTemplateID }

                },
                success: function (data) {
                    $('#dgDualList').datagrid('reload');
                    $('#dlgDelDualList').dialog('close');
                }
            });
        }
    }



</script>
</html>
